package org.fusionide.builder.parsers.cfml.tags;

import java.util.Enumeration;
import java.util.Vector;

import org.fusionide.builder.parsers.IParserHandler;
import org.fusionide.builder.parsers.ParserException;
import org.fusionide.builder.parsers.cfml.CFMLTag;
import org.fusionide.builder.parsers.cfml.Variable;

public class Function extends CFMLTag {
	// <cffunction name="" access="" description="" displayname="" hint=""
	// output="" returntype="" roles="">
	//
	// </cffunction>
	private String name;

	private String access;

	private String description;

	private String displayName;

	private String hint;

	private String output;

	private String returnType;

	private String roles;

	public Function(String attributes, int lineNumber) {
		super(attributes, lineNumber);

		if (attributes == null)
			return;

		this.name = getAttribute("name");
		this.access = getAttribute("access");
		this.description = getAttribute("description");
		this.displayName = getAttribute("displayname");
		this.hint = getAttribute("hint");
		this.output = getAttribute("output");
		this.returnType = getAttribute("returntype");
		this.roles = getAttribute("roles");
	}

	public boolean validate(IParserHandler parserHandler) {
		if (super.validate(parserHandler)) {
			if (name == null || name.equalsIgnoreCase("")) {
				if (parserHandler != null)
					parserHandler.addMarker(new ParserException("<cffunction> Name attribute is required", ParserException.SEVERITY_ERROR, startLine));
				return false;
			}
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean requiresClosingTag() {
		return true;
	}

	@Override
	public boolean acceptsChildren(CFMLTag tag) {
		if (super.acceptsChildren(tag)) {
			if (tag instanceof Function)
				return false;
			return true;
		}
		return false;
	}

	@Override
	public Vector<Variable> getVariables(int offset) {
		Vector<Variable> vars = new Vector<Variable>();

		vars.add(new Variable(name, startLine, true, Variable.function));

		return vars;
	}

	@Override
	public void closeTag(int offset, int linenumber, IParserHandler parserHandler) {
		super.closeTag(offset, linenumber, parserHandler);

		Vector<Variable> vars = getChildVariables(offset);
		vars.addAll(new CFPage().getVariables(1));
		vars = collapseVariableScope(vars);

		Enumeration<Variable> varEnum = vars.elements();
		while (varEnum.hasMoreElements()) {
			Variable var = varEnum.nextElement();
			if (!var.isVarScoped() && parserHandler != null)
				parserHandler.addMarker(new ParserException("Variable \"" + var.getName() + "\" not var scoped", ParserException.SEVERITY_WARNING, var
						.getLineNumber()));
		}

	}

	public String toString() {
		// <cffunction name="" access="" description="" displayname="" hint=""
		// output="" returntype="" roles="">
		StringBuffer sb = new StringBuffer();
		sb.append("<cffunction");
		sb.append(" name=\"" + name + "\"");
		if (access != null)
			sb.append(" access=\"" + access + "\"");
		if (description != null)
			sb.append(" description=\"" + description + "\"");
		if (displayName != null)
			sb.append(" displayname=\"" + displayName + "\"");
		if (hint != null)
			sb.append(" hint=\"" + hint + "\"");
		if (output != null)
			sb.append(" output=\"" + output + "\"");
		if (returnType != null)
			sb.append(" returntype=\"" + returnType + "\"");
		if (roles != null)
			sb.append(" hint=\"" + roles + "\"");

		sb.append(">");
		return sb.toString();
	}

	public String getAccess() {
		return access;
	}

	public void setAccess(String access) {
		this.access = access;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayname) {
		this.displayName = displayname;
	}

	public String getHint() {
		return hint;
	}

	public void setHint(String hint) {
		this.hint = hint;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOutput() {
		return output;
	}

	public void setOutput(String output) {
		this.output = output;
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returntype) {
		this.returnType = returntype;
	}

	public String getRoles() {
		return roles;
	}

	public void setRoles(String roles) {
		this.roles = roles;
	}
}
